package com.sfeir.poc.portal.gui.client;

import com.google.gwt.i18n.client.Dictionary;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.sfeir.poc.portal.gui.client.controller.Controller;
import com.sfeir.poc.portal.gui.client.dnd.IndexedPanel;
import com.sfeir.poc.portal.gui.client.services.NewsService;
import com.sfeir.poc.portal.gui.client.services.SpreadsheetService;
import com.sfeir.poc.portal.gui.client.services.WidgetService;
import com.sfeir.poc.portal.gui.client.util.WidgetBuilder;

public class Application extends Composite {

	//Main container of the application
	private Panel container = null;
	//Panel that allows drag and drop
	private IndexedPanel dndPanel = null;
	//Controller that dispatches events
	private Controller controller = new Controller();
	//Service that retrieves sheets title from spreadsheet id
	private SpreadsheetService spreadsheetService = new SpreadsheetService();
	//Service that retrieves data from spreadsheets
	private WidgetService widgetService = new WidgetService();
	//Service that retrieves news from spreadsheet Rss feed
	private NewsService newsService = new NewsService();
	//Class that build widgets according to their types
	private WidgetBuilder widgetBuilder = new WidgetBuilder();
	//Class that manages widgets from the current configuration
	private WidgetFactory widgetFactory = new WidgetFactory();

	private Dictionary args;

	private static Application defaultApplication;

	public Application() {
		defaultApplication = this;
		initArgs();

		//Main container for the all application
		container = new VerticalPanel();
		container.setSize("100%", "100%");
		initWidget(container);

		//Container for drag and drop
		dndPanel = new IndexedPanel();
		container.add(dndPanel);

		//Add event listeners to objects that needs
		dndPanel.addApplicationEventListener(getController());
		spreadsheetService.addApplicationEventListener(getController());
		widgetService.addApplicationEventListener(getController());
		newsService.addApplicationEventListener(getController());
		widgetFactory.addApplicationEventListener(getController());
		widgetBuilder.addApplicationEventListener(getController());

		//Used to init the DnD, should launch an event rather than doing that
		dndPanel.init();
	}

	public Controller getController(){
		return controller;
	}

	private void initArgs(){
		try {
			args = Dictionary.getDictionary("args");
		} catch (Exception e) {
			// It is not possible to do new Dictionnary("foo"), without a foo javacript variable,
			// so we need to 
			addArgsDictionary();
			args = Dictionary.getDictionary("args");
		}
	}

	native void addArgsDictionary() /*-{
	         $wnd["args"] = {
	         };
	      }-*/;

	public static Application getDefault(){
		return defaultApplication;
	}

	public String getArgs(String key) {
		String result = "";

		try {
			result = args.get(key);
		} catch (Exception e) {
		}

		return result; 
	}
}
